package com.drought.rained.utils;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

public class MaskUtilsLJ {
    private static Map<String, String> map = new HashMap<>();
    public static String MASK_STR = "  ***  ";

    public static void setText(EditText editText, String key, String text) {
        if (text.length() < 5) {
            editText.setText(text);
            return;
        }
        String newString = text.substring(0, 3) + MASK_STR + text.substring(text.length() - 2, text.length());
        editText.setText(newString);
        map.put(key, text);
    }

    public static void setText(TextView textView, String text) {
        if (text.length() < 5) {
            textView.setText(text);
            return;
        }
        String newString = text.substring(0, 3) + MASK_STR + text.substring(text.length() - 2, text.length());
        textView.setText(newString);
    }


    public static void setText(EditText editText, String key, String text, int start, int end) {
        if (end - start < text.length()) {
            editText.setText(text);
            return;
        }
        String newString = text.substring(0, start) + MASK_STR + text.substring(end, text.length());
        editText.setText(newString);
        map.put(key, text);
    }

    public static void setText(TextView textView, String key, String text, int start, int end) {
        if (end - start < text.length()) {
            textView.setText(text);
            return;
        }
        String newString = text.substring(0, start) + MASK_STR + text.substring(end, text.length());
        textView.setText(newString);
        map.put(key, text);
    }

    public static String getOriginText(String key, String text) {
        if (text.contains(MASK_STR)) {
            String origin = map.get(key);
            if (TextUtils.isEmpty(origin)) return text;
            return origin;
        }
        return text;
    }


    public static void clearText(View view) {
        try {
            TextView textView = (TextView) view;
            if (textView.getText().toString().contains(MASK_STR))
                textView.setText("");
        } catch (Exception e) {

        }
    }
}
